<?php
namespace app\admin\controller;
use think\Db;

class Interlocution extends Base {

    /**
     * 问答列表
     */
    public function index(){
        if(request()->isAjax()){
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('interlocution')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->count();//计算总页面
            $list = Db::name('interlocution')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->field('a.*,uuu.user_username')
                ->order('a.interlocution_num desc,interlocution_time desc')
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>$vo){
                if($vo['interlocution_img'] != ''){
                    $photo = explode (',',$vo['interlocution_img']);
                    $list[$key]['picture'] = $photo[0];
                    unset($photo[0]);
                    $list[$key]['interlocution_img'] = array_values($photo);
                }
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(session('?interlocution_id')){
            session('interlocution_id',null);
        }
        return $this->fetch();
    }

    /**
     * 问答查看详情
     */
    public function detail(){
        $id = input('param.id');
        $list = Db::name('interlocution')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('a.*,uuu.user_username')
            ->where('interlocution_id',$id)
            ->find();//计算总页面
        $photo = explode (',',$list['interlocution_img']);
        $this->assign('res',$list);
        $this->assign('interlocution_img',$photo);
        return $this->fetch();
    }

    /***
     * 问答删除
     */
    public function delete(){
        $ids = input('param.ids');
        Db::startTrans();// 启动事务
        $res = Db::table('up_interlocution')->where('interlocution_id',$ids)->delete();
        $count = Db::table('up_interlocution_answer')->where('f_interlocution_id',$ids)->count();
        if($count>0){
            Db::table('up_interlocution_answer')->where('f_interlocution_id',$ids)->delete();
             Db::table('up_interlocution')->where('interlocution_id',$ids)->setDec('interlocution_num',$count);

        }
        if($res){
            Db::commit();
            writelog(session('uid'),session('username'),'删除成功',1);
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            Db::rollback();
            writelog(session('uid'),session('username'),'删除失败',2);
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /**
     * 问答回复列表
     */
    public function answer(){
        if(request()->isAjax()){
            extract(input());
            $map = array();
            $map['f_interlocution_id'] = input('param.id');
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('interlocution_answer')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::name('interlocution_answer')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->field('a.*,uuu.user_username')
                ->order('a.answer_zang desc,answer_time desc')
                ->where($map)
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>$vo){
                if($vo['answer_img'] != ''){
                    $photo = explode (',',$vo['answer_img']);
                    $list[$key]['picture'] = $photo[0];
                    unset($photo[0]);
                    $list[$key]['answer_img'] = array_values($photo);
                }
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(!session('?interlocution_id')){
            session('interlocution_id',input('id'));
        }
        return $this->fetch();
    }

    /***
     * 问答回复编辑
     */
    public function update(){
        if(request()->isAjax()){

            $photo = input('input.photo');
            $answer_content = input('input.answer_content');
            $data['answer_content'] = $answer_content;
            $data['answer_img'] =$photo;
            $where['answer_id'] = input('param.id');
            Db::startTrans();// 启动事务
            $res = Db::table('up_interlocution_answer')->where($where)->update($data);
            if($res){
                Db::commit();
                writelog(session('uid'),session('username'),'删除成功',1);
                return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
            }else{
                Db::rollback();
                writelog(session('uid'),session('username'),'删除失败',2);
                return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
            }
        }
        $id = input('param.id');
        $list = Db::table('up_interlocution_answer')->where('answer_id',$id)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 问答回复删除
     */
    public function answerdelete(){
        $ids = input('param.ids');
        Db::startTrans();// 启动事务
        $list  =  Db::table('up_interlocution_answer')->where('answer_id',$ids)->find();
        $res =  Db::table('up_interlocution_answer')->where('answer_id',$ids)->delete();
        Db::table('up_interlocution')->where('interlocution_id',$list['f_interlocution_id'])->setDec('interlocution_num',1);
        if($res){
            Db::commit();
            writelog(session('uid'),session('username'),'删除成功',1);
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            Db::rollback();
            writelog(session('uid'),session('username'),'删除失败',2);
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }
}